package org.hcyspartnoc.luoxin.service.chat.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.context.BaseContextHolder;
import org.hcyspartnoc.luoxin.common.exception.chat.ChatSessionNoteException;
import org.hcyspartnoc.luoxin.common.exception.framework.AuthException;
import org.hcyspartnoc.luoxin.mapper.chat.ChatSessionNoteMapper;
import org.hcyspartnoc.luoxin.pojo.po.chat.ChatSessionNotePo;
import org.hcyspartnoc.luoxin.pojo.vo.chat.ChatSessionNoteVo;
import org.hcyspartnoc.luoxin.service.chat.IChatSessionNoteService;
import org.hcyspartnoc.luoxin.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

@Service
public class ChatSessionNoteServiceImpl
        extends ServiceImpl<ChatSessionNoteMapper, ChatSessionNotePo>
        implements IChatSessionNoteService {
    @Resource
    private ChatSessionNoteMapper chatSessionNoteMapper;

    @Override
    public PageBean<ChatSessionNotePo, ChatSessionNoteVo> queryPageByToken(int currentPage, int pageSize) {
        MPJLambdaWrapper<ChatSessionNotePo> wrapper = new MPJLambdaWrapper<ChatSessionNotePo>().eq(ChatSessionNotePo::getUserId, BaseContextHolder.getContext().getBaseCurrentUser().getId())
                .orderByDesc(ChatSessionNotePo::getCreateAt);
        Page<ChatSessionNotePo> page = new Page<>(currentPage, pageSize);
        chatSessionNoteMapper.selectPage(page, wrapper);
        ArrayList<ChatSessionNoteVo> chatSessionNoteVos = new ArrayList<>();
        for (ChatSessionNotePo po : page.getRecords()) {
            chatSessionNoteVos.add(notePoToVo(po));
        }
        return new PageBean<>(page, chatSessionNoteVos);
    }

    @Override
    public ChatSessionNoteVo queryById(Long id) throws ChatSessionNoteException, AuthException {
        ChatSessionNotePo chatSessionNotePo = noteNotExistsThrow(id);
        // 检查当前用户是否是手记拥有者
        if (!chatSessionNotePo.getUserId().equals(BaseContextHolder.getContext().getBaseCurrentUser().getId()))
            throw new AuthException(AuthException.PERMISSION_NOT_ENOUGH);
        return notePoToVo(chatSessionNotePo);
    }

    public static ChatSessionNoteVo notePoToVo(ChatSessionNotePo po) {
        ChatSessionNoteVo chatSessionNoteVo = BeanUtil.copyProperties(po, ChatSessionNoteVo.class);
        chatSessionNoteVo.setFormatCreateAt(StringUtils.formatDateTime(po.getCreateAt()));
        return chatSessionNoteVo;
    }


    private ChatSessionNotePo noteNotExistsThrow(Long id) throws ChatSessionNoteException {
        ChatSessionNotePo byId = getById(id);
        if (Objects.isNull(byId))
            throw new ChatSessionNoteException(ChatSessionNoteException.NOTE_NOT_EXISTS);
        return byId;
    }
}
